<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body><!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API 测试页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .section {
            margin-bottom: 30px;
            padding: 15px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        h2 {
            color: #333;
        }
        input, textarea, button {
            margin: 5px 0;
            padding: 8px;
            width: 100%;
            box-sizing: border-box;
        }
        button {
            background-color: #4CAF50;
            color: white;
            border: none;
            cursor: pointer;
        }
        button:hover {
            background-color: #45a049;
        }
        #response {
            margin-top: 10px;
            padding: 10px;
            background-color: #f8f8f8;
            border: 1px solid #ddd;
            border-radius: 3px;
            white-space: pre-wrap;
        }
    </style>
</head>
<body>
    <h1>Django JWT API 测试</h1>

    <div class="section">
        <h2>用户注册</h2>
        <input type="text" id="reg_username" placeholder="用户名">
        <input type="email" id="reg_email" placeholder="邮箱">
        <input type="password" id="reg_password" placeholder="密码">
        <input type="password" id="reg_password2" placeholder="确认密码">
        <input type="text" id="reg_first_name" placeholder="名字">
        <input type="text" id="reg_last_name" placeholder="姓氏">
        <button onclick="register()">注册</button>
        <div id="reg_response"></div>
    </div>

    <div class="section">
        <h2>用户登录</h2>
        <input type="text" id="login_username" placeholder="用户名或邮箱">
        <input type="password" id="login_password" placeholder="密码">
        <button onclick="login()">登录</button>
        <div id="login_response"></div>
    </div>

    <div class="section">
        <h2>访问受保护的路由</h2>
        <textarea id="access_token" rows="3" placeholder="Access Token"></textarea>
        <button onclick="accessProtected()">访问</button>
        <div id="protected_response"></div>
    </div>

    <div class="section">
        <h2>刷新 Token</h2>
        <textarea id="refresh_token" rows="3" placeholder="Refresh Token"></textarea>
        <button onclick="refreshToken()">刷新</button>
        <div id="refresh_response"></div>
    </div>

    <script>
        const baseURL = 'http://127.0.0.1:8000';

        function displayResponse(elementId, data) {
            document.getElementById(elementId).innerText = JSON.stringify(data, null, 2);
        }

        async function register() {
            const data = {
                username: document.getElementById('reg_username').value,
                email: document.getElementById('reg_email').value,
                password: document.getElementById('reg_password').value,
                password2: document.getElementById('reg_password2').value,
                first_name: document.getElementById('reg_first_name').value,
                last_name: document.getElementById('reg_last_name').value
            };

            try {
                const response = await fetch(`${baseURL}/api/auth/register/`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(data)
                });

                const result = await response.json();
                displayResponse('reg_response', result);
            } catch (error) {
                displayResponse('reg_response', { error: error.toString() });
            }
        }

        async function login() {
            const data = {
                username: document.getElementById('login_username').value,
                password: document.getElementById('login_password').value
            };

            try {
                const response = await fetch(`${baseURL}/api/auth/login/`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify(data)
                });

                const result = await response.json();
                displayResponse('login_response', result);

                if (response.ok) {
                    document.getElementById('access_token').value = result.access;
                    document.getElementById('refresh_token').value = result.refresh;
                }
            } catch (error) {
                displayResponse('login_response', { error: error.toString() });
            }
        }

        async function accessProtected() {
            const token = document.getElementById('access_token').value;

            if (!token) {
                displayResponse('protected_response', { error: '请提供 Access Token' });
                return;
            }

            try {
                const response = await fetch(`${baseURL}/api/auth/protected/`, {
                    method: 'GET',
                    headers: {
                        'Authorization': `Bearer ${token}`
                    }
                });

                const result = await response.json();
                displayResponse('protected_response', result);
            } catch (error) {
                displayResponse('protected_response', { error: error.toString() });
            }
        }

        async function refreshToken() {
            const token = document.getElementById('refresh_token').value;

            if (!token) {
                displayResponse('refresh_response', { error: '请提供 Refresh Token' });
                return;
            }

            try {
                const response = await fetch(`${baseURL}/api/token/refresh/`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ refresh: token })
                });

                const result = await response.json();
                displayResponse('refresh_response', result);

                if (response.ok) {
                    document.getElementById('access_token').value = result.access;
                }
            } catch (error) {
                displayResponse('refresh_response', { error: error.toString() });
            }
        }
    </script>
</body>
</html>


</body>
</html>